<?php
namespace Apipad\Controller;
use Think\Controller;

class CustomerController extends CommonController{


    //列表
    public function index()
    {
        $model = M('customer');
        $detail = I('post.');
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 15;
        $kewords = $detail['kewords'];
        $uid = UID;
        $where['status'] = 1;
        $where['aId'] = $uid;
        if ($kewords) {
            $where['mobile|customer_name'] = ['like', '%' . $kewords . '%'];
        }
        $where['deletetime'] = null;
        $count = $model->where($where)->count();
        $list = $model
            ->where($where)
            ->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = '读取成功';
        $data_cc['data'] = $list;
        $data_cc['count'] = $count;
        exit(json_encode($data_cc));
    }


    public function add()
    {
        $model = M('customer');
        $detail = I('post.');
        if($model->where(['mobile' => $detail['mobile']])->find()){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '已存在该账号！';
            exit(json_encode($data_cc));
        }
        $data = [
            'customer_name' => $detail['customer_name'],
            'add_time' => date('Y-m-d H:i:s'),
            'createtime' => time(),
            'high' => $detail['high'],
            'weight' => $detail['weight'],
            'sex' => $detail['sex'],
            'mobile' => $detail['mobile'],
            'region' => $detail['region'],
            'address' => $detail['address'],
            'address_detail' => $detail['address_detail'],
            'aId' => UID
        ];
        $ret = $model->add($data);
        if($ret){
            $data_cc['code'] = 200;
            $data_cc['msg'] = '新增成功';
            $data_cc['data'] = $ret;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = '新增失败';
            exit(json_encode($data_cc));
        }

    }


    public function edit()
    {
        $model = M('customer');
        $detail = I('post.');
        $id = $detail['id'];
        if($model->where(['mobile' => $detail['mobile']])->where(['id' => array('neq',$id)])->find()){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '已存在该账号！';
            exit(json_encode($data_cc));
        }
        $data = [
            'customer_name' => $detail['customer_name'],
            'updatetime' => time(),
            'high' => $detail['high'],
            'weight' => $detail['weight'],
            'sex' => $detail['sex'],
            'mobile' => $detail['mobile'],
            'region' => $detail['region'],
            'address' => $detail['address'],
            'address_detail' => $detail['address_detail'],
        ];
        $ret = $model->where(['id' =>$id])->save($data);
        if($ret){
            $data_cc['code'] = 200;
            $data_cc['msg'] = '修改成功';
            $data_cc['data'] = $ret;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = '修改失败';
            exit(json_encode($data_cc));
        }
    }

    public function del()
    {
        $model = M('customer');
        $detail = I('post.');
        $id = $detail['id'];
        $ret = $model->where(['id' => $id])->save(['deletetime' => time()]);
        if($ret){
            $data_cc['code'] = 200;
            $data_cc['msg'] = '修改成功';
            $data_cc['data'] = $ret;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = '修改失败';
            exit(json_encode($data_cc));
        }
    }
}